package com.hoppinzq.algorithm.graph;

import com.hoppinzq.algorithm.stackAndQueue.MyStack;

/**
 * 拓扑排序
 */
public class TopicOrder {
    private MyStack<Integer> stack;

    public TopicOrder(MyDigraph myDigraph){
        DirectedCycle directedCycle=new DirectedCycle(myDigraph);
        if(!directedCycle.isCycle()){
            DepthFirstOrder depthFirstOrder=new DepthFirstOrder(myDigraph);
            stack=depthFirstOrder.getOrder();
        }
    }

    public boolean isCycle(){
        return stack==null;
    }

    public int[] order(){
        int[] ints=new int[stack.length()];
        int s=stack.length();
        for(int i=0;i<s;i++){
            if(stack.isEmpty()){
                break;
            }
            ints[i]=stack.pop();
        }
        return ints;
    }

    public MyStack<Integer> orderS(){
        return stack;
    }
}
